package com.pandy.disruptor.quickstart;

import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

/**
 * @description: 生产者组件
 * @author: panhong
 * @date: 2025/4/10
 */
public class OrderEventProducer {

    private RingBuffer<OrderEvent> ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void sendData(ByteBuffer data) {
        // 从RingBuffer中获取一个可用序号
        long sequence = ringBuffer.next();
        try {
            // 根据这个序号获取对象，此时的对象是一个没有赋值的空对象
            OrderEvent orderEvent = ringBuffer.get(sequence);
            // 赋值
            orderEvent.setOderId(data.getLong(0));
        } finally {
            // 提交发布操作
            ringBuffer.publish(sequence);
        }
    }
}
